/* 
 *  Syntelos XB
 *  Copyright (C) 2006 John Pritchard.
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 *  02111-1307 USA
 */

package syntelos.xb.op;

/**
 * <p> JVM instruction <code>0xab lookupswitch</code>. </p>
 */
public final class Lookupswitch
    extends syntelos.xb.Op.Switch
{
    public final static syntelos.iou.dom.Name NAME = new syntelos.iou.dom.impl.Name(ELEMENT_NODE,"http://www.syntelos.org/xb/op","xop:lookupswitch");
    public final static int code = CODE_LOOKUPSWITCH;
    public final static java.lang.String name = "lookupswitch";
    public final static int size_operands = 10;
    public final static int count_stack_pop = 1;
    public final static int count_stack_push = 0;

    /**
     * <p> Child of {@link syntelos.xb.Op} . </p>
     * 
     * @see syntelos.xb.at.Code
     */
    public final static class Case
	extends syntelos.xb.op.Operand.Case
	implements java.lang.Comparable
    {
	public final static syntelos.iou.dom.Name NAME = new syntelos.iou.dom.impl.Name(ELEMENT_NODE,"http://www.syntelos.org/xb/op","xop:lookupswitch.case");

	public Case(org.w3c.dom.Document doc, syntelos.iou.dom.Name name){
	    super(doc,name);
	}
	public Case(org.w3c.dom.Document doc){
	    this(doc,NAME);
	}
	public Case(){
	    this(DOC_NIL);
	}

	public void readCF(syntelos.xb.Op.Reader in)
	    throws java.io.IOException
	{
	    this.setId(in.readInt());
	    this.setOperand(in.readInt());
	}
	public void writeCF(syntelos.iou.bbuf out){
	    out.write4(this.getId());
	    out.write4(this.getOperand());
	}
    }



    public Lookupswitch(org.w3c.dom.Document doc, syntelos.iou.dom.Name type){
        super(doc,type);
    }
    public Lookupswitch(){
        super(NAME,false);
    }
    public Lookupswitch(boolean shared){
        super(NAME,true);
    }


    public syntelos.xb.op.Operand.Case getOperandCaseNode(int key){
	java.lang.Integer number = new java.lang.Integer(key);
	return (Case)this.getChildById(number,this);
    }
    protected Case getCase(int idx){
	return (Case)this.getChildByIndex(idx);
    }
    public syntelos.xb.op.Operand.Case getCreateOperandCaseNode(int key){
	Case switchcase = (Case)this.getOperandCaseNode(key);
	if (null == switchcase){
	    switchcase = new Case();
	    this.appendChild(switchcase);
	    switchcase.setId(key);
	}
	return switchcase;
    }
    public int getCode(){
        return code;
    }
    public java.lang.String getName(){
        return name;
    }
    public int sizeOperands(){
	int acc = Pad(this.getPC());
	acc += 8;
	int count = this.getChildNodes2().getLength();
	acc += (8 * count);
	return acc;
    }
    public int countStackPop(){
        return count_stack_pop;
    }
    public int countStackPush(){
        return count_stack_push;
    }
    public void readCF(syntelos.xb.Class clas, syntelos.xb.Method meth, syntelos.xb.Op.Reader in)
        throws java.io.IOException
    {
        if (this.setPC(in)){
	    int pc = this.getPC();
	    in.mark(java.lang.Integer.MAX_VALUE);
	    int pad = Pad(pc);
	    in.skip(pad);
	    this.getOperandCaseDefaultAttr().readCF(in);
	    /*
	     */
	    int count = in.readInt();
	    Case switchcase;
	    for (int cc = 0; cc < count; cc++){
		switchcase = new Case();
		this.appendChild(switchcase);
		switchcase.readCF(in);
	    }
	    /*
	     * Save input bits 
	     */
	    byte[] cf = in.marked();
	    this.setCF(cf);
        }
        else
            throw new syntelos.xb.Error.Bug();
    }
    public void writeCF(syntelos.xb.Class clas, syntelos.xb.Method meth, syntelos.xb.Op.Writer out)
        throws java.io.IOException
    {
        if (this.setPC(out)){
	    int pc = this.getPC();
	    int pad = Pad(pc);
	    syntelos.iou.bbuf cf_buf = new syntelos.iou.bbuf();
	    cf_buf.skip_write(pad);
	    this.getOperandCaseDefaultAttr().writeCF(cf_buf);
	    /*
	     */
	    syntelos.iou.dom.NodeList children = this.getChildNodes2();
	    children.sort();
	    int count = children.getLength();
	    cf_buf.write4(count);
	    Case switchcase;
	    for (int cc = 0; cc < count; cc++){
		switchcase = this.getCase(cc);
		switchcase.writeCF(cf_buf);
	    }
	    /*
	     * Save output bits 
	     */
	    byte[] cf = cf_buf.toByteArray();
	    this.setCF(cf);
	    out.write(cf);
        }
        else
            throw new syntelos.xb.Error.Bug();
    }
}
